//
// Created by xm on 2024/5/6.
//

#include <sstream>
#include "Person.h"
#include "iostream"

namespace Test04 {

    Person::Person() {
        name = (char *) malloc(sizeof(""));
        strcpy(name,"");
        age = 0;
        height = 0;
    };
    Person::Person(const char *_name, int _age, float _height) {
        name = (char *) malloc(sizeof(strlen(_name) + 1));
        strcpy(name,_name);
        age = _age;
        height = _height;
    };
    Person::Person(const char *_name, int _age) {
        name = (char *) malloc(sizeof(strlen(_name) + 1));
        strcpy(name,_name);
        age = _age;
        height = 0.0;
    };
    Person::Person(const char *_name) {
        name = (char *) malloc(sizeof(strlen(_name) + 1));
        strcpy(name,_name);
        age = 0;
        height = 0.0;
    };
    // 拷贝构造
    Person::Person(const Person& person) {
        name = (char *) malloc(sizeof(person.name));
        strcpy(name,person.name);
        age = person.age;
        height = person.height;
    };
    Person::~Person(){
        if(name != NULL){
            free(name);
            name = NULL;
        }

        std::cout << "destruct Person<" << this <<">" << std::endl;
    }
    void Person::setName(const char* _name){
        name = (char *) malloc(sizeof(strlen(_name) + 1));
        strcpy(name,_name);
    }

    std::string Person::description() const
    {
        std::stringstream addr;
        addr << this;

       return "Person<" + addr.str() + ">\n" + "name:" + name + "\nage:" +std::to_string(age) + "\nheight:" + std::to_string(height) + "\n";
    }
    Person &Person::getPerson(){
        return *this;
    }

    void Person::toPrint() {
        std::cout << this->description() << std::endl;
    }

    PersonSmartPoint::PersonSmartPoint(Person *p) {
        this->pPerson = p;
    }

    PersonSmartPoint::~PersonSmartPoint(){
        std::cout << "destruct PersonSmartPoint<" << this <<">" << std::endl;
        if(this->pPerson != NULL){
            delete this->pPerson;
            this->pPerson = NULL;
        }
    }

} // test04
